//
//  DDBossActivenessViewController.m
//  DingDongZP
//
//  Created by weily on 16/5/14.
//  Copyright © 2016年 iyhjiang.com. All rights reserved.
//

#import "DDBossActivenessViewController.h"
#import "DDBossInfoService.h"
#import "DDCompanyMicroPageViewController.h"
#import "DDNewCattleViewController.h"

static NSString *const kBossSignDateKey = @"ad2234123125532";

@interface DDBossActivenessViewController ()



@end

@implementation DDBossActivenessViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = COLOR_BACKGROUND_GRAY;
    // Do any additional setup after loading the view.
    
    ///	设置相关的同显示
    self.shareLab.text = @"分享微主页";
    self.cattleLab.text = @"看看新牛人";
    
    ///	活跃度从用户信息处取到
    self.activenessLab.text = self.bossInfo.activeness;
    
    ///	被关注率
    self.attentionLab.text = [NSString stringWithFormat:@"活跃度越高，排名越靠前，越易被Boss关注\n你的被关注度超过了%ld%%的牛人", self.bossInfo.attention.integerValue];
    
    [[DDBossInfoService sharedService] queryBossInfoWithRefresh:NO callback:^(DDBossInfo *info) {
        NSString *url = [DDAPIGenerate imageURLWithPath:info.logo];
        [self.avatarImageView sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamed:@"common_avatar"]];
    }];
    
    [self checkIsTimeToSignUp];
}


#pragma mark - functions
- (void)checkIsTimeToSignUp
{
    ///	签到逻辑
    NSDate *lastSignTime = [[NSUserDefaults standardUserDefaults] objectForKey:kBossSignDateKey];
    if (lastSignTime && [lastSignTime timeIntervalSinceNow] < 60*60*24) {
        self.signUpBtn.selected = YES;
    } else {
        self.signUpBtn.selected = NO;
    }
}

- (void)setSignUpTime
{
    [[NSUserDefaults standardUserDefaults] setObject:[NSDate date] forKey:kBossSignDateKey];
}


/// 签到
- (IBAction)checkIn:(UIButton *)sender
{
    if (sender.selected) {
        return;
    }
    [self showLoading];
    __weak typeof(self) weakSelf = self;
    
    [[DDBossInfoService sharedService] addActivenessWithType:kActivitySignIn callback:^(BOOL succ, id data) {
        
        [weakSelf hideLoading];
        if (succ) {
            [weakSelf showText:@"签到成功"];
            sender.selected = YES;
            [weakSelf setSignUpTime];
        }
        else {
            [weakSelf showText:@"签到失败"];
        }
    }];
}

/// 沟通
- (IBAction)comunicateAction:(UIButton *)sender {
    
    DDNewCattleViewController *vc = [[DDNewCattleViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

/// 看看新牛人
- (IBAction)lookoverAction:(UIButton *)sender {
    
    DDNewCattleViewController *vc = [[DDNewCattleViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

/// 分享微主页
- (IBAction)shareAction:(UIButton *)sender {
    
    [self showLoading];
    __weak typeof(self) weakSelf = self;
    
    [[DDBossInfoService sharedService] addActivenessWithType:kActivityShare callback:^(BOOL succ, id data) {
        
        [weakSelf hideLoading];
        if (succ) {
            DDCompanyMicroPageViewController *vc = [[DDCompanyMicroPageViewController alloc] init];
            vc.isShare = YES;
            [weakSelf.navigationController pushViewController:vc animated:YES];
        }
        else {
            [weakSelf showText:data];
        }
    }];
}


@end
